/**
 * Created by Jackie.Chen on 2016/12/4.
 */

var modelComment = function () {
    /*基础配置*/
    var el = 'body';
    var addLay = null,updateLay = null;
    var searchObj = {
        status:'',
        startTime:'',
        endTime:'',
        pageNum: 1,
        pageSize: 10
    };

    /*操作*/
    function doSearch() {
        var status= $("#status").attr('data');
        var startTime = $('#startDate').val();
        var endTime = $('#endDate').val();
        searchObj.status = status || '';
        searchObj.startTime = startTime || '';
        searchObj.endTime = endTime || '';
        searchObj.pageNum = 1;  //reset
        renderPages(true);
    }










    function renderPages(isFirst) {
        var url = '/modelController/model_comment.do', isAdd = false;
        getHtmlByUrl({
            url: url,
            data:searchObj,
            success: function (res) {
                $("#modelComment").html(res);
                if(isFirst){
                    buildPageArea();
                }
            }
        });
    }
    /*end页面初始化和事件绑定*/


    function getOptionValue(e){
        var $me = $(this);
        $(this).parents('.dropdown-menu').siblings('.dropdown-toggle').removeClass('error').siblings('.error').remove();
        $me.parents('.dropdown-menu').siblings('.dropdown-toggle').val($me.find('a').text());
        $me.parents('.dropdown-menu').siblings('.dropdown-toggle').attr("data",$me.attr("data"));
    }
    /*分页*/
    function doChangePage(num) {
        searchObj.page = num;
        renderPages();
    }
    function buildPageArea() {
        var pageAllCount = $('#pageAllCount').val(),
            totalPage = Math.ceil(pageAllCount/searchObj.pageSize);
        laypage({
            cont: $('#pageD'),
            pages: totalPage,
            curr: searchObj.page,
            totalCount:pageAllCount,
            skin: '#338af9',
            jump: function(obj, first){
                if(!first){
                    doChangePage(obj.curr);
                }
            }
        });
    }
    /*end分页*/
    /*页面初始化和事件绑定*/
    function bindEvents() {
        $(el).undelegate()
            .delegate('.dropdown-menu li','click',getOptionValue)
            .delegate('#searchBtn','click',doSearch);
    }

    /*页面初始化*/
    function init() {
        renderPages(true);
        bindEvents();
        /**日历*/
        $('#searchTimeRange').daterangepicker();
        $('#searchTimeRange').on('apply.daterangepicker',function(ev,picker){
            $("#startDate").val(picker.startDate.format('YYYY-MM-DD'));
            $("#endDate").val(picker.endDate.format('YYYY-MM-DD'));

        });

        /*日历end*/
    }
    /*end 页面初始化*/
    init();
};
